package GUILayer;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.* ;
import java.util.*;
import ControlLayer.*;

/**
 * Write a description of class UIPerson here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UIPerson
{
    private AddressCtr addressControler;
    
    private    JLabel idLabel = new JLabel("ID       ");
    private     JLabel nameLabel = new JLabel("Name");
    private    JLabel addressLabel = new JLabel("Address");
    private    JLabel idDeleteLabel = new JLabel("ID");
    private    JLabel postalCodeLabel=new JLabel ("Post C. ");
    private    JLabel cityLabel=new JLabel ("City      ");
    private   JLabel phoneLabel=new JLabel ("Phone  ");
        
    private   JTextField idText = new JTextField(10);
    private    JTextField nameText = new JTextField(10);
    private   JTextField addressText = new JTextField(15);
    private   JTextField postalCodeText=new JTextField(10);
    private   JTextField cityText=new JTextField(10);
    private   JTextField phoneText=new JTextField(10);
    private    JTextField idDeleteText = new JTextField(10);

    /**
     * Constructor for objects of class UIPerson
     */
    public UIPerson()
    {
     addressControler=new AddressCtr();
    }

    public JPanel getPersonsPanel(JFrame frame1)
    {
        final JFrame frame=frame1;
        JPanel panelMain=new JPanel();
        panelMain.setLayout(new BoxLayout(panelMain, BoxLayout.Y_AXIS));
        
        
        JButton addPerson = new JButton("Add Person");
        JButton deletePerson = new JButton("Delete Person");
        JButton showPerson= new JButton("Show information");
        JButton showAllPersons= new JButton("Show All Persons");
      
    
      
        
        
        addPerson.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { addPerson(frame); }
                           });
                        
        deletePerson.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { deletePerson (frame); }
                           });     
                         
        showPerson.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { showPerson(frame); }
                           });                          
        
       showAllPersons.addActionListener(new ActionListener() {
                               public void actionPerformed(ActionEvent e) { showAllPersons(frame) ;}
                           });   
        
        panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        
        
     
        JPanel panelName=new JPanel();
        panelName.add(nameLabel);
        panelName.add(nameText);
        addComponent(panelName,panelMain);
        
        JPanel panelID=new JPanel();
        panelID.add(idLabel);
        panelID.add(idText);
        addComponent(panelID,panelMain);
        
        JPanel panelAddress=new JPanel();
        panelAddress.add(addressLabel);
        panelAddress.add(addressText);
        addComponent(panelAddress,panelMain);
        
        JPanel panelPostalCode=new JPanel();
        panelPostalCode.add(postalCodeLabel);
        panelPostalCode.add(postalCodeText);
        addComponent(panelPostalCode,panelMain);
        
        JPanel panelCity=new JPanel();
        panelCity.add(cityLabel);
        panelCity.add(cityText);
        addComponent(panelCity,panelMain);
        
        JPanel panelPhone=new JPanel();
        panelPhone.add(phoneLabel);
        panelPhone.add(phoneText);
        addComponent(panelPhone,panelMain);
        
 
        addComponent(addPerson,panelMain);
         panelMain.add(Box.createRigidArea(new Dimension(300,15)));
     
        JPanel panelDeletePerson=new JPanel();
        panelDeletePerson.add(idDeleteLabel);
        panelDeletePerson.add(idDeleteText);
        addComponent(panelDeletePerson,panelMain);
        
       
       JPanel panelOptionButtons=new JPanel(); 
       panelOptionButtons.add(deletePerson);
       panelOptionButtons.add(showPerson);
        addComponent(panelOptionButtons,panelMain);
        panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        
        addComponent(showAllPersons,panelMain);
        panelMain.add(Box.createRigidArea(new Dimension(300,5)));
        
       return panelMain;
    }
    
     private void addPerson(JFrame frame)
    {
          try
          {
           long id=Long.valueOf(idText.getText()) ;
    
           addressControler.createPerson(id,nameText.getText(),addressText.getText(),
           postalCodeText.getText(),cityText.getText(),phoneText.getText());
           idText.setText("");
           nameText.setText("");
           addressText.setText("");
           postalCodeText.setText("");
           cityText.setText("");
           phoneText.setText("");
          
          JOptionPane.showMessageDialog(frame,"Person added successfuly.");
          }
          
          catch(Exception e)
          {
            JOptionPane.showMessageDialog(frame,"Error while adding person,please try again!.","",JOptionPane.ERROR_MESSAGE);
            }

    }
    
    private void deletePerson(JFrame frame)
    {
        try
        {
         long  id=Long.valueOf(idDeleteText.getText()) ;

         int choice=JOptionPane.showConfirmDialog(frame,"Do you really want to delete this person?", "", JOptionPane.YES_NO_OPTION);
           if(choice==0)
           {
           addressControler.deletePerson(id);
           idDeleteText.setText("");
           JOptionPane.showMessageDialog(frame,"Person deleted successfuly.");
           }
        }
        
        catch(Exception e)
        {
          JOptionPane.showMessageDialog(frame,"Wrong ID,please try again!.","",JOptionPane.ERROR_MESSAGE);
        }
    }
    
    
    /**
     * Show the information about the person according to the ID
     * writen in the text field.
     */
    private void showPerson(JFrame frame)
    {
       
        try
        {
        long  id=Long.valueOf(idDeleteText.getText());  
        String[] columnNames = {"Name","ID","Address","Post C.", "City","Phone"};
        Object [][] data={ {addressControler.getPersonName(id),id,addressControler.getPersonAddress(id)
            ,addressControler.getPersonPostalCode(id),addressControler.getPersonCity(id),addressControler.getPersonPhone(id)}};
   
          idDeleteText.setText("");  
        JTable table = new JTable(data, columnNames);
        JFrame inFrame=new JFrame();
        inFrame.setLayout(new BorderLayout());
        inFrame.add(table.getTableHeader(), BorderLayout.PAGE_START);
        inFrame.add(table, BorderLayout.CENTER);
        
        inFrame.pack();
        inFrame.setVisible(true);
       }
       
       
       
       catch(Exception e)
       {
        JOptionPane.showMessageDialog(frame,"Wrong ID,please try again!.","",JOptionPane.ERROR_MESSAGE);
       }

    }
    
    private void showAllPersons(JFrame frame)
    {
     String[] columnNames = {"Name","ID","Address","Post C.", "City","Phone"};
     ArrayList<Long> ids=addressControler.getPersonsIDs();
     ArrayList<String> names=addressControler.getPersonsNames();
     ArrayList<String> adresses=addressControler.getPersonsAddress();
     ArrayList<String> postalCodes=addressControler.getPersonsPostalCodes();
     ArrayList<String> cities=addressControler.getPersonsCities();
     ArrayList<String> phones=addressControler.getPersonsPhones();
     Object [][] data=new Object [ids.size()][6];
     
        for(int i=0;i<ids.size();i++)
            {
            data[i][0]=names.get(i);
            data[i][1]=ids.get(i);
            data[i][2]=adresses.get(i);
            data[i][3]=postalCodes.get(i);
            data[i][4]=cities.get(i);
            data[i][5]=phones.get(i);
            }
            
        JTable table = new JTable(data, columnNames);
        JFrame inFrame=new JFrame();
        inFrame.setLayout(new BorderLayout());
        inFrame.add(table.getTableHeader(), BorderLayout.PAGE_START);
        inFrame.add(table, BorderLayout.CENTER);
        
        inFrame.pack();
        inFrame.setVisible(true);
    }
    
     private static void addComponent(JComponent component, Container container)
    {
        component.setAlignmentX(Component.CENTER_ALIGNMENT);
        container.add(component);
        
    }
 
}
